home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
JFC.bin
/
MetalComboBoxButton.java
< prev
next >
Wrap
Text File
|
1998-06-30
|
5KB
|
157 lines
/*
* @(#)MetalComboBoxButton.java 1.13 98/04/21
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
package com.sun.java.swing.plaf.metal;
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.plaf.basic.*;
import com.sun.java.swing.*;
import com.sun.java.swing.plaf.*;
import com.sun.java.swing.border.*;
import java.io.Serializable;
/**
* JButton subclass to help out MetalComboBoxUI
* <p>
* Warning: serialized objects of this class will not be compatible with
* future swing releases. The current serialization support is appropriate
* for short term storage or RMI between Swing1.0 applications. It will
* not be possible to load serialized Swing1.0 objects with future releases
* of Swing. The JDK1.2 release of Swing will be the compatibility
* baseline for the serialized form of Swing objects.
*
* @see MetalComboBoxButton
* @version 1.13 04/21/98
* @author Tom Santos
*/
public class MetalComboBoxButton extends JButton {
protected JComboBox comboBox;
protected JList listBox;
protected CellRendererPane rendererPane;
protected Icon comboIcon;
protected boolean iconOnly = false;
public final JComboBox getComboBox() { return comboBox;}
public final void setComboBox( JComboBox cb ) { comboBox = cb;}
public final Icon getComboIcon() { return comboIcon;}
public final void setComboIcon( Icon i ) { comboIcon = i;}
public final boolean isIconOnly() { return iconOnly;}
public final void setIconOnly( boolean isIconOnly ) { iconOnly = isIconOnly;}
MetalComboBoxButton() {
super( "" );
DefaultButtonModel model = new DefaultButtonModel() {
public void setArmed( boolean armed ) {
super.setArmed( isPressed() ? true : armed );
}
};
setModel( model );
}
public MetalComboBoxButton( JComboBox cb, Icon i,
CellRendererPane pane, JList list ) {
this();
comboBox = cb;
comboIcon = i;
rendererPane = pane;
listBox = list;
}
public MetalComboBoxButton( JComboBox cb, Icon i, boolean onlyIcon,
CellRendererPane pane, JList list ) {
this( cb, i, pane, list );
iconOnly = onlyIcon;
}
public void paintComponent( Graphics g ) {
// Paint the button as usual
super.paintComponent( g );
Insets insets = getInsets();
int width = getWidth() - (insets.left + insets.right);
int height = getHeight() - (insets.top + insets.bottom);
int left = insets.left;
int top = insets.top;
int right = left + (width - 1);
int bottom = top + (height - 1);
int iconWidth = 0;
int iconLeft = right;
// Paint the icon
if ( comboIcon != null ) {
iconWidth = comboIcon.getIconWidth();
int iconHeight = comboIcon.getIconHeight();
int iconTop = 0;
if ( iconOnly ) {
iconLeft = (getWidth() / 2) - (iconWidth / 2);
iconTop = (getHeight() / 2) - (iconHeight / 2);
}
else {
iconLeft = (left + (width - 1)) - iconWidth;
iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2);
}
comboIcon.paintIcon( this, g, iconLeft, iconTop );
// Paint the focus
if ( hasFocus() ) {
g.setColor( MetalLookAndFeel.getFocusColor() );
g.drawRect( left - 1, top - 1, width + 3, height + 1 );
}
}
// Let the renderer paint
if ( ! iconOnly && comboBox != null ) {
ListCellRenderer renderer = comboBox.getRenderer();
Component c;
boolean renderPressed = getModel().isPressed();
c = renderer.getListCellRendererComponent(listBox,
comboBox.getSelectedItem(),
-1,
renderPressed,
false);
c.setFont(rendererPane.getFont());
if ( model.isArmed() && model.isPressed() ) {
if ( isOpaque() ) {
c.setBackground(UIManager.getColor("Button.pressed"));
}
}
else {
c.setBackground(comboBox.getBackground());
}
c.setForeground(comboBox.getForeground());
int cWidth = width - (insets.right + iconWidth);
rendererPane.paintComponent( g, c, this, left, top, cWidth, height );
}
}
}